<template>
  <div class="cateContainer">
    <el-select v-if="!isText" v-model="selCode" clearable placeholder="请选择" size="mini" style="width:100px;"  @change="select">
      <el-option style="font-size:12px"
        v-for="item in options"
        :key="item.code"
        :label="item.name"
        :value="item.code">
      </el-option>
    </el-select>
    <el-input style="width:100px;" :maxlength="maxlength"
      size="mini"
      v-if="isText"
      :placeholder="placeHolder"
      v-model="txtCode"
      clearable>
    </el-input>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        options: [],
        txtCode: "",
        selCode: ""
      }
    },
    props:{
      maxlength: {
        type: Number,
        default: 1
      },
      placeHolder:{
        type: String,
        default: '输入编码'
      }
    },
    methods: {
      bindData (options) {
        this.selCode = "";
        this.options=options;
      },
      getItem(){
        if(!this.isText){
          return this.options.find(x=>x.code == this.selCode);
        }
        return {};
      },
      getCode(){
        if(this.isText){
          return this.txtCode;
        }
        else{
          return parseInt(this.selCode)>=0?this.selCode:'';
        }
      },
      putPlaceHolder(placeHolder){
        this.placeHolder = placeHolder
      },
      select(item) {
        console.log("select:"+item);
        this.$emit('selectChange', item.code);
      }
    },
    computed:{
      isText: {
        get () {
          if(this.options.length>0){
              return false;
          }
          return true;
        }
      },
    }
  }
</script>
<style>
  .cateContainer {
    float:left;
  }
</style>
